package aynu.hd.Login.LoginController;

import aynu.hd.Login.LoginService.loginService;
import aynu.hd.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    loginService loginService;

    //登录页面
    @RequestMapping({"/", "/login"})
    public String index(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if(user!=null){
            if(user.getUser_admin().equals("管理员")){
                return "redirect:/admin/room/index";
            }
            return "redirect:/hotel/index";
        }
        return "login/login";
    }

    //登录验证
    @PostMapping("userLogin")
    @ResponseBody
    public String userLogin(User user, HttpSession session) {
        String data = loginService.queryUserByName(user, session);
        return data;
    }

    //退出登录
    @GetMapping("userBack")
    public String userBack(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/login";
    }

    //注册页面跳转
    @GetMapping("registerUser")
    public String registerUser() {
        return "hotel/user/register";
    }

    //获得注册验证码
    @GetMapping("verification")
    @ResponseBody
    public String verification(String user_email, HttpSession session) {
        String data = loginService.queryUserByEmail(user_email, session);
        return data;
    }

    //注册验证
    @PostMapping("useRegister")
    @ResponseBody
    public String useRegister(User user, String code, HttpSession session) {
        String data = loginService.addUser(user, code, session);
        return data;
    }

    //找回密码
    @GetMapping("forget")
    public String forget() {
        return "hotel/user/forget";
    }

    //获得找回密码验证码
    @GetMapping("verificationForget")
    @ResponseBody
    public String verificationForget(String user_email, String user_name, HttpSession session) {
        String data = loginService.queryUserByEmailForget(user_email, user_name, session);
        return data;
    }

    //更改新密码
    @PostMapping("userLoginForget")
    @ResponseBody
    public String userLoginForget(User user, String code, HttpSession session) {
        String data = loginService.userLoginForget(user, code, session);
        return data;
    }
}
